#include "NetToolKits.h"

#pragma comment(lib,"NetToolKits.lib")

#define localh "10.175.13.88"
class MyProcessor :IocpServerDelegate{
public:
	virtual void onRead(IocpClient * client, byte *buffer, int len){
		memset(buffer + len, 0, client->wsRecvBuff.len - len);
		cout << string((const char*)buffer) << endl;
		if (client->connectSocket != NULL){
			int res = client->sendAsync((const byte*)("hello"),0,6);
			if (res == -1){
				cout << "send failed!" << endl;
			}
		}
	}
	virtual void onAccept(IocpClient* client){
		SOCKADDR_IN sockAddr;
		int namelen = sizeof(sockAddr);
		getpeername(client->connectSocket, (struct sockaddr*)&sockAddr, &namelen);
		cout << inet_ntoa(sockAddr.sin_addr) << " : " << sockAddr.sin_port << " connected !" << endl;
	}
	virtual void onClose(IocpClient* client){
		SOCKADDR_IN sockAddr;
		int namelen = sizeof(sockAddr);
		getpeername(client->connectSocket, (struct sockaddr*)&sockAddr, &namelen);
		cout << inet_ntoa(sockAddr.sin_addr) << " : " << sockAddr.sin_port << " disconnected !" << endl;
	}
};
int main(){
	WinSockDamon sockdamon;
	IocpServer server;
	MyProcessor *pr = new MyProcessor();
	server.setProcessor((IocpServerDelegate*)pr);
	if (server.startListen(string(localh), 5050, 10)){
		cout << "server started at localh: 5050" << endl;
	}
	else
	{
		cout << "server started failed" << endl;
	}
	int i;
	cin >> i;
	return 0;
}